import { useUserInfoStore } from "@/stores/userInfo";
import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import HomeView from "../views/HomeView.vue";
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: HomeView,
      meta: { requiresAuth: true },
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/Login.vue"),
      meta: { requiresAuth: false },
    },
    {
      path: "/gongsi",
      name: "gongsi",
      meta: { requiresAuth: true },
      children: [
        {
          path: "/gongsi/DeptInfo",
          name: "DeptInfo",
          component: () =>
            import("@/views/CorporateInformationManagement/DeptInfo.vue"),
          meta: { requiresAuth: true },
        },
        {
          path: "/gongsi/EmployeeInformation",
          name: "EmployeeInformation",
          component: () =>
            import(
              "@/views/CorporateInformationManagement/EmployeeInformation.vue"
            ),
          meta: { requiresAuth: true },
        },
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  const store = useUserInfoStore();
  if (to.path === "/login") next();
  if (!localStorage.getItem('token')) {
    if (to.path != "/login") {
      next({ path: "/login" });
    } else {
      next();
    }
  } else {
    next();
  }
});
export default router;
